package com.test.lotterysystem.Service.Impl;

import com.test.lotterysystem.Comment.Constants.Constants;
import com.test.lotterysystem.Comment.ErrorCode.ServiceErrorCode;
import com.test.lotterysystem.Comment.Exception.ServiceException;
import com.test.lotterysystem.Comment.Utils.JacksonUtil;
import com.test.lotterysystem.Controller.Param.DrawPrizeParam;
import com.test.lotterysystem.Dao.DaoObject.ActivityDO;
import com.test.lotterysystem.Dao.DaoObject.ActivityPrizeDO;
import com.test.lotterysystem.Dao.DaoObject.PrizeDO;
import com.test.lotterysystem.Dao.Mapper.ActivityMapper;
import com.test.lotterysystem.Dao.Mapper.ActivityPrizeMapper;
import com.test.lotterysystem.Dao.Mapper.PrizeMapper;
import com.test.lotterysystem.Service.DrawPrizeService;
import com.test.lotterysystem.Service.Enum.ActivityEnum;
import com.test.lotterysystem.Service.Enum.ActivityPrizeEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@Service
@Slf4j
public class DrawPrizeServiceImpl implements DrawPrizeService {
    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Autowired
    private ActivityMapper activityMapper;

    @Autowired
    private ActivityPrizeMapper activityPrizeMapper;

    /**
     * 发送数据到rabbitMQ中
     * @param drawPrizeParam
     */
    @Override
    public void saveDrawPrizeInfo(DrawPrizeParam drawPrizeParam) {
        if (drawPrizeParam == null) {
            throw new ServiceException(ServiceErrorCode.DRAW_PRIZE_INFO_IS_EMPTY);
        }
        Map<String, String> map = new HashMap<>();
        map.put("messageId", UUID.randomUUID().toString().replace("-",""));
        map.put("message", JacksonUtil.writeValueAsString(drawPrizeParam));

        // 发送数据到rabbitMQ中
        rabbitTemplate.convertAndSend(Constants.EXCHANGE_NAME, Constants.KEY, map);
        log.info("信息传递成功...{}",map);
    }

    /**
     * 校验rabbitMQ中获得的消息的格式
     * @param drawPrizeParam
     * @return
     */
    @Override
    public boolean checkDrawPrizeMessage(DrawPrizeParam drawPrizeParam) {
        if (drawPrizeParam == null) {
            log.error(ServiceErrorCode.DRAW_PRIZE_PARAM_IS_EMPTY.getErrMsg());
            return false;
        }

        // 校验活动和奖品存在
        ActivityDO activityDO = activityMapper.getActivityDetail(drawPrizeParam.getActivityId());
        ActivityPrizeDO activityPrizeDO = activityPrizeMapper.getActivityPrize(drawPrizeParam.getActivityId(), drawPrizeParam.getPrizeId());
        if (activityPrizeDO == null || activityDO == null) {
            log.error(ServiceErrorCode.DRAW_PRIZE_ACTIVITY_OR_PRIZE_IS_EMPTY.getErrMsg());
            return false;
        }

        // 校验活动状态
        if(!activityDO.getStatus().equalsIgnoreCase(ActivityEnum.PROCEED.name())){
            log.error(ServiceErrorCode.ACTIVITY_STATUS_ERROR.getErrMsg());
            return false;
        }

        // 校验奖品状态
        if (!activityPrizeDO.getStatus().equalsIgnoreCase(ActivityPrizeEnum.INIT.name())) {
            log.error(ServiceErrorCode.PRIZE_STATUS_ERROR.getErrMsg());
            return false;
        }

        // TODO 校验人员状态

        // 中奖者人数是否和设置奖品数量一致
        if(drawPrizeParam.getWinnerList().size()!=activityPrizeDO.getPrizeAmount()){
            log.error(ServiceErrorCode.WINNER_PRIZE_AMOUNT_ERROR.getErrMsg());
            return false;
        }
        return true;
    }
}
